#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDebug>
#include <QSerialPortInfo>
#include <QSerialPort>
#include <QMessageBox>
#include <QTime>
#include <QDateTime>
MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    SlotsInit();
    GetAvailablePorts(ui->comboBox);
    GetAvailablePorts(ui->comboBox_3);
    GetAvailablePorts(ui->comboBox_4);
    GetAvailablePorts(ui->comboBox_5);
    GetAvailablePorts(ui->comboBox_6);
    GetAvailablePorts(ui->comboBox_7);
    GetAvailablePorts(ui->comboBox_8);
    GetAvailablePorts(ui->comboBox_9);

    ui->listWidget->setCurrentRow(0);
    ui->listWidget_2->setCurrentRow(0);
    ui->listWidget_3->setCurrentRow(0);
    ui->listWidget_4->setCurrentRow(0);

}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::SlotsInit()
{
    connect(ui->listWidget_2,SIGNAL(itemSelectionChanged()),this,SLOT(ItemSelectChanged()));
    connect(ui->listWidget,SIGNAL(itemSelectionChanged()),this,SLOT(ListWidget1ItemSelectChanged()));

    connect(ui->pushButton,SIGNAL(clicked()),this,SLOT(OpenPort()));
    connect(ui->pushButton_2,SIGNAL(clicked()),this,SLOT(SendReadData()));

    connect(&timer,SIGNAL(timeout()),this,SLOT(SendReadData()));
    connect(ui->checkBox,SIGNAL(clicked()),this,SLOT(CheckBoxChecked()));

}

void MainWindow::GetAvailablePorts(QComboBox * combo)
{
    for(QSerialPortInfo x:QSerialPortInfo::availablePorts())
    {
        combo->addItem(x.portName());
    }
}

void MainWindow::GetByteArray4(QByteArray & sendData)
{
    if(ui->listWidget->currentRow()==0x00)
        sendData.append(ui->listWidget_4->currentRow()+0x33);
    else if(ui->listWidget->currentRow()==0x02)
    {
        if(ui->listWidget_2->currentRow()<7)
        {
            sendData.append((uchar)0x00+0x33);
        }
        else if(ui->listWidget_2->currentRow()>=7)
        {
            if(ui->listWidget_4->currentRow()<=5)
            {
                sendData.append(ui->listWidget_4->currentRow()+0x33);
            }
            else if(ui->listWidget_4->currentRow()==0x06)
            {
                sendData.append(0x0a+0x33);
            }
        }
    }
}

void MainWindow::GetByteArray3(QByteArray &sendData)
{
    if(ui->listWidget->currentRow()==0)
    {
        if(ui->listWidget_3->currentRow()==5)
        {
            sendData.append((uchar)(0xff+0x33));
        }
        else
            sendData.append(ui->listWidget_3->currentRow()+0x33);
    }
    else if(ui->listWidget->currentRow()==0x02)
    {
        if(ui->listWidget_2->currentRow()<=1)
        {
            if(ui->listWidget_3->currentRow()<=2)
                sendData.append(ui->listWidget_3->currentRow()+1+0x33);
            else if(ui->listWidget_3->currentRow()==0x03)
            {
                sendData.append((uchar)(0xff+0x33));
            }
        }
        else if(ui->listWidget_2->currentRow()>=2&&ui->listWidget_2->currentRow()<=5)
        {
            if(ui->listWidget_3->currentRow()<=3)
            {
                sendData.append(ui->listWidget_3->currentRow()+0x33);
            }
            else if(ui->listWidget_3->currentRow()==0x04)
            {
                sendData.append((uchar)(0xff+0x33));
            }
        }
        else if(ui->listWidget_2->currentRow()==0x06)
        {
            if(ui->listWidget_3->currentRow()<=2)
            {
                sendData.append(ui->listWidget_3->currentRow()+1+0x33);
            }
            else if(ui->listWidget_3->currentRow()==3)
            {
                sendData.append((uchar)(0xff+0x33));
            }

        }
        else if(ui->listWidget_2->currentRow()==0x07)
        {
            sendData.append((uchar)0x00+0x33);
        }
    }
}

void MainWindow::GetByteArray2(QByteArray &sendData)
{
    if(ui->listWidget->currentRow()==0)
    {
        if(ui->listWidget_2->currentRow()<=10)
        {
            sendData.append(ui->listWidget_2->currentRow()+0x33);
        }

        if(ui->listWidget_2->currentRow()>=12&&ui->listWidget_2->currentRow()<=19)
        {
            sendData.append(ui->listWidget_2->currentRow()+0x09+0x33);
        }

        if(ui->listWidget_2->currentRow()>=21&&ui->listWidget_2->currentRow()<=28)
        {
            sendData.append(ui->listWidget_2->currentRow()+20+0x33);
        }

        if(ui->listWidget_2->currentRow()>=30&&ui->listWidget_2->currentRow()<=37)
        {
            sendData.append(ui->listWidget_2->currentRow()+31+0x33);
        }
    }
    else if(ui->listWidget->currentRow()==0x02)
    {
        if(ui->listWidget_2->currentRow()<=0x06)
        {
            sendData.append(ui->listWidget_2->currentRow()+1+0x33);
        }
        else if(ui->listWidget_2->currentRow()==0x07)
        {
            sendData.append((uchar)(0x80+0x33));
        }

    }
}

void MainWindow::GetByteArray1(QByteArray &sendData)
{
    sendData.append(ui->listWidget->currentRow()+0x33);
}

void MainWindow::AddCheckSum(QByteArray &sendData)
{
    unsigned char checkSum=0;
    for(int i=0;i<=sendData.length()-1;i++)
    {
        checkSum=checkSum+(uchar)sendData.data()[i];
    }
    sendData.append(checkSum);
    sendData.append(0x16);
}

void MainWindow::ReadData(QByteArray & readData,int i)
{
    readData.clear();
    QDateTime start=QDateTime::currentDateTime();
    while (start.msecsTo(QDateTime::currentDateTime())<3000)
    {
        QApplication::processEvents();
        if(dlt645[i].readyRead.count()>=1)
        {
            dlt645[i].vectorLocker.lock();
            dlt645[i].readyRead.pop_front();
            dlt645[i].vectorLocker.unlock();

            QByteArray data=dlt645[i].port.readAll();
            readData.append(data);
        }
        if(readData.length()>0)
            if(readData.data()[readData.length()-1]==0x16)
                break;
    }
}

void MainWindow::ParseRecvData(QByteArray recvData)
{
    if(recvData.data()[0]!=0x68)
        return;
    QByteArray data;
    data.clear();
    data.append(recvData[1]);
    data.prepend(recvData.data()[2]);
    data.prepend(recvData.data()[3]);
    data.prepend(recvData.data()[4]);
    data.prepend(recvData.data()[5]);
    data.prepend(recvData.data()[6]);
    qDebug()<<data.toHex();
    qDebug()<<__FUNCTION__<<__LINE__<<(uchar)recvData.data()[7];
    if((uchar)recvData.data()[7]!=0x68)
        return;
    qDebug()<<__FUNCTION__<<__LINE__;
    if((uchar)recvData.data()[8]!=0x91&&(uchar)recvData.data()[8]!=0xb1)
        return;
    qDebug()<<__FUNCTION__<<__LINE__;
    int dataLength=recvData.data()[9];
    qDebug()<<dataLength;
    if(recvData.data()[13]-0x33==0x00)
    {
        if(dataLength==8)
        {
            QByteArray data;
            data.clear();
            data.append(recvData.data()[9+5]-0x33);
            QString showvalue=data.toHex();
            showvalue.prepend(".");
            data.clear();
            data.append(recvData.data()[9+6]-0x33);
            showvalue.prepend(data.toHex());

            data.clear();
            data.append(recvData.data()[9+7]-0x33);
            showvalue.prepend(data.toHex());

            data.clear();
            data.append(recvData.data()[9+8]-0x33);
            showvalue.prepend(data.toHex());

            float value=showvalue.toFloat();
            QString units="kWh";
            if(recvData.data()[12]-0x33==(0x09)||recvData.data()[12]-0x33==0x0a)
                units="KVAh";
            //if()
            qDebug()<<value;
            ui->textEdit->setText(QString::number(value,'f',2)+units);
        }
        else if(dataLength==24)
        {
            ui->textEdit->clear();
            for(int i=0;i<=4;i++)
            {
                QByteArray data;
                data.clear();
                data.append(recvData.data()[9+5+4*i]-0x33);
                QString showvalue=data.toHex();
                showvalue.prepend(".");
                data.clear();
                data.append(recvData.data()[9+6+4*i]-0x33);
                showvalue.prepend(data.toHex());

                data.clear();
                data.append(recvData.data()[9+7+4*i]-0x33);
                showvalue.prepend(data.toHex());

                data.clear();
                data.append(recvData.data()[9+8+4*i]-0x33);
                showvalue.prepend(data.toHex());

                float value=showvalue.toFloat();
                qDebug()<<value;
                ui->textEdit->append(QString::number(value,'f',2)+"kWh");
            }
        }
    }
    else if(recvData.data()[13]-0x33==0x02)
    {
        qDebug()<<"变量数据标识"<<recvData.data()[12]-0x33<<dataLength;
        //D2=0x01;
        if(recvData.data()[12]-0x33==0x01&&dataLength==6)
        {
            ParseDetail(recvData,14,2,1,"V",3,1);
        }
        else if(recvData.data()[12]-0x33==0x01&&dataLength==10)
        {
            ParseDetail(recvData,14,2,3,"V",3,1);
        }
        else if(recvData.data()[12]-0x33==0x02&&dataLength==7)
        {
            ParseDetail(recvData,14,3,1,"A",3,3);
        }
        else if(recvData.data()[12]-0x33==0x02&&dataLength==13)
        {
            ParseDetail(recvData,14,3,3,"A",3,3);
        }
        else if((recvData.data()[12]-0x33==0x03||recvData.data()[12]-0x33==0x04||recvData.data()[12]-0x33==0x05)&&dataLength==7)
        {
            ParseDetail(recvData,14,3,1,"kW",2,4);
        }
        else if((recvData.data()[12]-0x33==0x03||recvData.data()[12]-0x33==0x04||recvData.data()[12]-0x33==0x05)&&dataLength==16)
        {
            ParseDetail(recvData,14,3,4,"kW",2,4);
        }
        else if((recvData.data()[12]-0x33==0x06)&&(dataLength==6))
        {
            ParseDetail(recvData,14,2,1,"分",1,3);
        }
        else if((recvData.data()[12]-0x33==0x06)&&(dataLength==12))
        {
            ParseDetail(recvData,14,2,4,"分",1,3);
        }
        else if((recvData.data()[12]-0x33==0x07)&&(dataLength==6))
        {

            ParseDetail(recvData,14,2,1,"°",3,1);
            qDebug()<<"相角";
        }
        else if((recvData.data()[12]-0x33==0x07)&&(dataLength==10))
        {
            ParseDetail(recvData,14,2,3,"°",3,1);
            qDebug()<<"相角";
        }
        else if(((uchar)(recvData.data()[12]-0x33)==0x80))
        {
            if(recvData.data()[10]-0x33==0x00)
            {
                ParseDetail(recvData,14,2,1,"V",3,1);
            }
            else if(recvData.data()[10]-0x33==0x01)
            {
                ParseDetail(recvData,14,3,1,"A",3,3);
            }
            else if(recvData.data()[10]-0x33==0x02)
            {
                ParseDetail(recvData,14,2,1,"Hz",2,2);
            }
            else if(recvData.data()[10]-0x33==0x03)
            {
                ParseDetail(recvData,14,3,1,"kW",2,4);
            }
            else if(recvData.data()[10]-0x33==0x04)
            {
                ParseDetail(recvData,14,3,1,"kW",2,4);
            }
            else if(recvData.data()[10]-0x33==0x05)
            {
                ParseDetail(recvData,14,3,1,"kvar",2,4);
            }
            else if(recvData.data()[10]-0x33==0x0a)
            {
                QString showValue="";
                QByteArray data;
                data.clear();
                data.append(recvData.data()[14]-0x33);
                showValue.append(data.toHex());
                data.clear();
                data.append(recvData.data()[15]-0x33);
                showValue.prepend(data.toHex());
                data.clear();
                data.append(recvData.data()[16]-0x33);
                showValue.prepend(data.toHex());
                data.clear();
                data.append(recvData.data()[17]-0x33);
                showValue.prepend(data.toHex());
                long value=showValue.toLong();
                ui->textEdit->setText(QString::number(value));
            }
        }
    }
}

void MainWindow::ParseRecvData(QByteArray recvData, int portID,int fourLoop)

{
    if(recvData.data()[0]!=0x68)
        return;
    QByteArray data;
    data.clear();
    data.append(recvData[1]);
    data.prepend(recvData.data()[2]);
    data.prepend(recvData.data()[3]);
    data.prepend(recvData.data()[4]);
    data.prepend(recvData.data()[5]);
    data.prepend(recvData.data()[6]);
    qDebug()<<data.toHex();
    qDebug()<<__FUNCTION__<<__LINE__<<(uchar)recvData.data()[7];
    if((uchar)recvData.data()[7]!=0x68)
        return;
    qDebug()<<__FUNCTION__<<__LINE__;
    if((uchar)recvData.data()[8]!=0x91&&(uchar)recvData.data()[8]!=0xb1)
        return;
    qDebug()<<__FUNCTION__<<__LINE__;
    int dataLength=recvData.data()[9];
    qDebug()<<dataLength;

    if(portID==7)
        last8=true;

    if(recvData.data()[13]-0x33==0x00)
    {
        if(dataLength==8)
        {
            QByteArray data;
            data.clear();
            data.append(recvData.data()[9+5]-0x33);
            QString showvalue=data.toHex();
            showvalue.prepend(".");
            data.clear();
            data.append(recvData.data()[9+6]-0x33);
            showvalue.prepend(data.toHex());

            data.clear();
            data.append(recvData.data()[9+7]-0x33);
            showvalue.prepend(data.toHex());

            data.clear();
            data.append(recvData.data()[9+8]-0x33);
            showvalue.prepend(data.toHex());

            float value=showvalue.toFloat();
            QString units="kWh";
            if(recvData.data()[12]-0x33==(0x09)||recvData.data()[12]-0x33==0x0a)
                units="KVAh";
            //if()
            qDebug()<<value;
            if(portID==0&&last8==true)
            {
                last8=false;
                ui->textEdit->clear();
            }
            ui->textEdit->append(QString("串口%0回路%1:").arg(portID).arg(fourLoop)+QString::number(value,'f',2)+units);
        }
        else if(dataLength==24)
        {
            for(int i=0;i<=4;i++)
            {
                QByteArray data;
                data.clear();
                data.append(recvData.data()[9+5+4*i]-0x33);
                QString showvalue=data.toHex();
                showvalue.prepend(".");
                data.clear();
                data.append(recvData.data()[9+6+4*i]-0x33);
                showvalue.prepend(data.toHex());

                data.clear();
                data.append(recvData.data()[9+7+4*i]-0x33);
                showvalue.prepend(data.toHex());

                data.clear();
                data.append(recvData.data()[9+8+4*i]-0x33);
                showvalue.prepend(data.toHex());

                float value=showvalue.toFloat();
                qDebug()<<value;
                if(portID==0&&last8==true)
                {
                    last8=false;
                    ui->textEdit->clear();
                }
                ui->textEdit->append(QString("串口%0回路%1:").arg(portID).arg(fourLoop)+QString::number(value,'f',2)+"kWh");
            }
        }
    }
    else if(recvData.data()[13]-0x33==0x02)
    {
        qDebug()<<"变量数据标识"<<recvData.data()[12]-0x33<<dataLength;
        //D2=0x01;
        if(recvData.data()[12]-0x33==0x01&&dataLength==6)
        {
            ParseDetail(recvData,14,2,1,"V",3,1,portID,fourLoop);
        }
        else if(recvData.data()[12]-0x33==0x01&&dataLength==10)
        {
            ParseDetail(recvData,14,2,3,"V",3,1,portID,fourLoop);
        }
        else if(recvData.data()[12]-0x33==0x02&&dataLength==7)
        {
            ParseDetail(recvData,14,3,1,"A",3,3,portID,fourLoop);
        }
        else if(recvData.data()[12]-0x33==0x02&&dataLength==13)
        {
            ParseDetail(recvData,14,3,3,"A",3,3,portID,fourLoop);
        }
        else if((recvData.data()[12]-0x33==0x03||recvData.data()[12]-0x33==0x04||recvData.data()[12]-0x33==0x05)&&dataLength==7)
        {
            ParseDetail(recvData,14,3,1,"kW",2,4,portID,fourLoop);
        }
        else if((recvData.data()[12]-0x33==0x03||recvData.data()[12]-0x33==0x04||recvData.data()[12]-0x33==0x05)&&dataLength==16)
        {
            ParseDetail(recvData,14,3,4,"kW",2,4,portID,fourLoop);
        }
        else if((recvData.data()[12]-0x33==0x06)&&(dataLength==6))
        {
            ParseDetail(recvData,14,2,1,"分",1,3,portID,fourLoop);
        }
        else if((recvData.data()[12]-0x33==0x06)&&(dataLength==12))
        {
            ParseDetail(recvData,14,2,4,"分",1,3,portID,fourLoop);
        }
        else if((recvData.data()[12]-0x33==0x07)&&(dataLength==6))
        {

            ParseDetail(recvData,14,2,1,"°",3,1,portID,fourLoop);
            qDebug()<<"相角";
        }
        else if((recvData.data()[12]-0x33==0x07)&&(dataLength==10))
        {
            ParseDetail(recvData,14,2,3,"°",3,1,portID,fourLoop);
            qDebug()<<"相角";
        }
        else if(((uchar)(recvData.data()[12]-0x33)==0x80))
        {
            if(recvData.data()[10]-0x33==0x00)
            {
                ParseDetail(recvData,14,2,1,"V",3,1,portID,fourLoop);
            }
            else if(recvData.data()[10]-0x33==0x01)
            {
                ParseDetail(recvData,14,3,1,"A",3,3,portID,fourLoop);
            }
            else if(recvData.data()[10]-0x33==0x02)
            {
                ParseDetail(recvData,14,2,1,"Hz",2,2,portID,fourLoop);
            }
            else if(recvData.data()[10]-0x33==0x03)
            {
                ParseDetail(recvData,14,3,1,"kW",2,4,portID,fourLoop);
            }
            else if(recvData.data()[10]-0x33==0x04)
            {
                ParseDetail(recvData,14,3,1,"kW",2,4,portID,fourLoop);
            }
            else if(recvData.data()[10]-0x33==0x05)
            {
                ParseDetail(recvData,14,3,1,"kvar",2,4,portID,fourLoop);
            }
            else if(recvData.data()[10]-0x33==0x0a)
            {
                QString showValue="";
                QByteArray data;
                data.clear();
                data.append(recvData.data()[14]-0x33);
                showValue.append(data.toHex());
                data.clear();
                data.append(recvData.data()[15]-0x33);
                showValue.prepend(data.toHex());
                data.clear();
                data.append(recvData.data()[16]-0x33);
                showValue.prepend(data.toHex());
                data.clear();
                data.append(recvData.data()[17]-0x33);
                showValue.prepend(data.toHex());
                long value=showValue.toLong();
                if(portID==0&&last8==true)
                {
                    last8=false;
                    ui->textEdit->clear();
                }
                ui->textEdit->append(QString("串口%0回路%1:").arg(portID).arg(fourLoop)+QString::number(value));
            }
        }
    }
}

void MainWindow::ParseDetail(QByteArray recvData, int start, int oneGroup, int gropNumber, QString unit, int decimal, int decimalPoint)
{
    QString showValue="";
    //    if(portid==0)
    //    {
    //        ui->textEdit->clear();
    //    }
    for(int j=0;j<gropNumber;j++)
    {
        showValue.clear();
        for(int i=0;i<=oneGroup-1;i++)
        {
            uchar dataVlue=recvData.data()[start+i+j*oneGroup]-0x33;
            if(i<oneGroup-1)
            {
                QByteArray data;
                data.resize(0);
                data.clear();
                data.append(dataVlue);
                showValue.prepend(data.toHex());

            }
            else if(i==oneGroup-1)
            {
                if((dataVlue&0x80)==0x80)
                {
                    dataVlue=dataVlue&0x7f;
                    QByteArray  data;
                    data.clear();
                    data.append(dataVlue);
                    showValue.prepend(data.toHex());
                    showValue.insert(decimal,".");
                    showValue.prepend("-");
                    float realValue=showValue.toFloat();
                    ui->textEdit->append(QString::number(realValue,'f',decimalPoint)+unit);
                }
                else
                {
                    QByteArray data;
                    data.clear();
                    data.append(dataVlue);
                    showValue.prepend(data.toHex());
                    showValue.insert(decimal,".");
                    float realValue=showValue.toFloat();
                    ui->textEdit->append(QString::number(realValue,'f',decimalPoint)+unit);
                }
            }
        }
    }
}

void MainWindow::ParseDetail(QByteArray recvData, int start, int oneGroup, int gropNumber, QString unit, int decimal, int decimalPoint, int portid,int fourLoop)
{
    QString showValue="";
    if(portid==0&&last8==true)
    {
        last8=false;
        ui->textEdit->clear();
    }
    for(int j=0;j<gropNumber;j++)
    {
        showValue.clear();
        for(int i=0;i<=oneGroup-1;i++)
        {
            uchar dataVlue=recvData.data()[start+i+j*oneGroup]-0x33;
            if(i<oneGroup-1)
            {
                QByteArray data;
                data.resize(0);
                data.clear();
                data.append(dataVlue);
                showValue.prepend(data.toHex());

            }
            else if(i==oneGroup-1)
            {
                if((dataVlue&0x80)==0x80)
                {
                    dataVlue=dataVlue&0x7f;
                    QByteArray  data;
                    data.clear();
                    data.append(dataVlue);
                    showValue.prepend(data.toHex());
                    showValue.insert(decimal,".");
                    showValue.prepend("-");
                    float realValue=showValue.toFloat();
                    ui->textEdit->append(QString("串口%0回路%1:").arg(portid).arg(fourLoop)+QString::number(realValue,'f',decimalPoint)+unit);
                }
                else
                {
                    QByteArray data;
                    data.clear();
                    data.append(dataVlue);
                    showValue.prepend(data.toHex());
                    showValue.insert(decimal,".");
                    float realValue=showValue.toFloat();
                    ui->textEdit->append(QString("串口%0回路%1:").arg(portid).arg(fourLoop)+QString::number(realValue,'f',decimalPoint)+unit);
                }
            }
        }
    }
}

void MainWindow::CheckBoxChecked()
{
    if(ui->checkBox->isChecked())
    {
        timer.start(ui->spinBox->value());
    }
    else
        timer.stop();
}
void MainWindow::ItemSelectChanged()
{
    qDebug()<<__FUNCTION__<<ui->listWidget_2->currentRow();
    if(ui->listWidget->currentRow()==0)
    {
        if(ui->listWidget_2->currentRow()>=12)
        {
            ui->listWidget_3->clear();
            ui->listWidget_3->addItem("0x00(总)");
            ui->listWidget_3->setCurrentRow(0);
        }
        else
        {
            ui->listWidget_3->clear();
            ui->listWidget_3->addItem("0x00(有功总电能)");
            ui->listWidget_3->addItem("0x01(费率1电能)");
            ui->listWidget_3->addItem("0x02(费率2电能)");
            ui->listWidget_3->addItem("0x03(费率3电能)");
            ui->listWidget_3->addItem("0x04(费率4电能)");
            ui->listWidget_3->addItem("0xff(数据块)");
            ui->listWidget_3->setCurrentRow(0);
        }
    }
    else if(ui->listWidget->currentRow()==2)
    {
        if(ui->listWidget_2->currentRow()==0)
        {
            ui->listWidget_3->clear();
            ui->listWidget_3->addItem("0x01(A相电压)");
            ui->listWidget_3->addItem("0x02(B相电压)");
            ui->listWidget_3->addItem("0x03(C相电压)");
            ui->listWidget_3->addItem("0xff(电压数据块)");
            ui->listWidget_3->setCurrentRow(0);
            ui->listWidget_4->clear();
            ui->listWidget_4->addItem("0x00(总)");
            ui->listWidget_4->setCurrentRow(0);
        }
        else if(ui->listWidget_2->currentRow()==1)
        {
            ui->listWidget_3->clear();
            ui->listWidget_3->addItem("0x01(A相电流)");
            ui->listWidget_3->addItem("0x02(B相电流)");
            ui->listWidget_3->addItem("0x03(C相电流)");
            ui->listWidget_3->addItem("0xff(电流数据块)");
            ui->listWidget_3->setCurrentRow(0);
            ui->listWidget_4->clear();
            ui->listWidget_4->addItem("0x00(总)");
            ui->listWidget_4->setCurrentRow(0);
        }
        else if(ui->listWidget_2->currentRow()==2)
        {
            ui->listWidget_3->clear();
            ui->listWidget_3->addItem("0x00(瞬时总有功功率)");
            ui->listWidget_3->addItem("0x01(A相瞬时有功功率)");
            ui->listWidget_3->addItem("0x03(B相瞬时有功功率)");
            ui->listWidget_3->addItem("0x04(C相瞬时有功功率)");
            ui->listWidget_3->addItem("0xff(瞬时有功功率数据块)");
            ui->listWidget_3->setCurrentRow(0);
            ui->listWidget_4->clear();
            ui->listWidget_4->addItem("0x00(总)");
            ui->listWidget_4->setCurrentRow(0);
        }
        else if(ui->listWidget_2->currentRow()==3)
        {
            ui->listWidget_3->clear();
            ui->listWidget_3->addItem("0x00(瞬时总无功功率)");
            ui->listWidget_3->addItem("0x01(A相瞬时无功功率)");
            ui->listWidget_3->addItem("0x03(B相瞬时无功功率)");
            ui->listWidget_3->addItem("0x04(C相瞬时无功功率)");
            ui->listWidget_3->addItem("0xff(瞬时无功功率数据块)");
            ui->listWidget_3->setCurrentRow(0);
            ui->listWidget_4->clear();
            ui->listWidget_4->addItem("0x00(总)");
            ui->listWidget_4->setCurrentRow(0);
        }
        else if(ui->listWidget_2->currentRow()==4)
        {
            ui->listWidget_3->clear();
            ui->listWidget_3->addItem("0x00(瞬时总视在功率)");
            ui->listWidget_3->addItem("0x01(A相瞬时视在功率)");
            ui->listWidget_3->addItem("0x03(B相瞬时视在功率)");
            ui->listWidget_3->addItem("0x04(C相瞬时视在功率)");
            ui->listWidget_3->addItem("0xff(瞬时视在功率数据块)");
            ui->listWidget_3->setCurrentRow(0);
            ui->listWidget_4->clear();
            ui->listWidget_4->addItem("0x00(总)");
            ui->listWidget_4->setCurrentRow(0);
        }
        else if(ui->listWidget_2->currentRow()==5)
        {
            ui->listWidget_3->clear();
            ui->listWidget_3->addItem("0x00(总功率因数)");
            ui->listWidget_3->addItem("0x01(A相功率因数)");
            ui->listWidget_3->addItem("0x03(B相功率因数)");
            ui->listWidget_3->addItem("0x04(C相功率因数)");
            ui->listWidget_3->addItem("0xff(功率因数数据块)");
            ui->listWidget_3->setCurrentRow(0);
            ui->listWidget_4->clear();
            ui->listWidget_4->addItem("0x00(总)");
            ui->listWidget_4->setCurrentRow(0);
        }
        else if(ui->listWidget_2->currentRow()==6)
        {
            ui->listWidget_3->clear();
            ui->listWidget_3->addItem("0x01(A相相角)");
            ui->listWidget_3->addItem("0x02(B相相角)");
            ui->listWidget_3->addItem("0x03(C相相角)");
            ui->listWidget_3->addItem("0xff(相角数据块)");
            ui->listWidget_3->setCurrentRow(0);
            ui->listWidget_4->clear();
            ui->listWidget_4->addItem("0x00(总)");
            ui->listWidget_4->setCurrentRow(0);
        }
        else if(ui->listWidget_2->currentRow()==7)
        {
            ui->listWidget_3->clear();
            ui->listWidget_3->addItem("0x00(零线电流)");
            ui->listWidget_3->setCurrentRow(0);
            ui->listWidget_4->clear();
            ui->listWidget_4->addItem("0x00(零地电压)");
            ui->listWidget_4->addItem("0x01(零线电流)");
            ui->listWidget_4->addItem("0x02(电网频率)");
            ui->listWidget_4->addItem("0x03(一分钟内有功总平均功率)");
            ui->listWidget_4->addItem("0x04(当前有功需量)");
            ui->listWidget_4->addItem("0x05(当前无功需量)");
            ui->listWidget_4->addItem("0x0a(内部电池工作时间)");
            ui->listWidget_4->setCurrentRow(0);
        }
    }
}

void MainWindow::OpenPort()
{

    if(ui->pushButton->text()=="打开端口")
    {
        dlt645[0].SetPort(ui->comboBox->currentText(),ui->comboBox_2->currentText().toInt());
        dlt645[1].SetPort(ui->comboBox_3->currentText(),ui->comboBox_2->currentText().toInt());
        dlt645[2].SetPort(ui->comboBox_4->currentText(),ui->comboBox_2->currentText().toInt());
        dlt645[3].SetPort(ui->comboBox_5->currentText(),ui->comboBox_2->currentText().toInt());
        dlt645[4].SetPort(ui->comboBox_6->currentText(),ui->comboBox_2->currentText().toInt());
        dlt645[5].SetPort(ui->comboBox_7->currentText(),ui->comboBox_2->currentText().toInt());
        dlt645[6].SetPort(ui->comboBox_8->currentText(),ui->comboBox_2->currentText().toInt());
        dlt645[7].SetPort(ui->comboBox_9->currentText(),ui->comboBox_2->currentText().toInt());
        int portCnt=0;
        for(int i=0;i<=7;i++)
        {
            dlt645[i].port.open(QSerialPort::ReadWrite);
            if(!dlt645[i].port.isOpen())
            {
                QMessageBox a;
                a.setText(dlt645[i].port.portName()+"打开失败");
                a.show();
                a.exec();
            }
            else
            {
                portCnt++;
            }
        }
        if(portCnt==8)
        {
            QMessageBox a;
            a.setText("打开成功");
            a.show();
            a.exec();
        }
        ui->pushButton->setText("关闭端口");

    }
    else if(ui->pushButton->text()=="关闭端口")
    {

        int closeCnt=0;
        for(int i=0;i<=7;i++)
        {
            dlt645[i].port.close();
            if(dlt645[i].port.isOpen())
            {
                QMessageBox a;
                a.setText(dlt645[i].port.portName()+"关闭失败");
                a.show();
                a.exec();
            }
            else
                closeCnt++;
            ui->pushButton->setText("打开端口");
        }
        if(closeCnt==8)
        {
            QMessageBox a;
            a.setText("关闭成功");
            a.show();
            a.exec();
        }
    }
    qDebug()<<__FUNCTION__;
}

void MainWindow::SendReadData()
{
    qDebug()<<__FUNCTION__;
    ui->textEdit->clear();
    if(dlt645[0].port.isOpen())
    {

        QString fourLoop[4];
        fourLoop[0]=ui->lineEdit->text();
        fourLoop[1]=ui->lineEdit_2->text();
        fourLoop[2]=ui->lineEdit_3->text();
        fourLoop[3]=ui->lineEdit_4->text();
        for (int i=0;i<=7;i++)
        {

            for(int j=0;j<=3;j++)
            {
                QByteArray SendData;
                SendData.clear();
                SendData.append(0x68);
                QString adder=fourLoop[j];
                QString tmp[6];
                for(int k=0;k<=5;k++)
                {
                    tmp[k]=adder.right(2);
                    adder.remove(adder.length()-2,2);
                    SendData.append(QByteArray::fromHex(tmp[k].toUtf8()));
                    qDebug()<<adder<<tmp[k];
                }

                SendData.append(0x68);
                SendData.append(0x11);
                SendData.append(0x04);
                GetByteArray4(SendData);
                GetByteArray3(SendData);
                GetByteArray2(SendData);
                GetByteArray1(SendData);
                AddCheckSum(SendData);
                qDebug()<<"send:"<<SendData.toHex();

                dlt645[i].port.write(SendData);
                dlt645[i].port.waitForBytesWritten();
                QByteArray recvData;
                recvData.clear();
                ReadData(recvData,i);
                qDebug()<<"recv:"<<recvData.toHex();
                ParseRecvData(recvData,i,j);
            }

        }
    }
    else
    {
        QMessageBox a;
        a.setText("端口未打开");
        a.show();
        a.exec();
    }
}

void MainWindow::ListWidget1ItemSelectChanged()
{
    if(ui->listWidget->currentRow()==1)
    {
        ui->listWidget_2->clear();
        ui->listWidget_3->clear();
        ui->listWidget_4->clear();
    }
    else if(ui->listWidget->currentRow()==0)
    {
        ui->listWidget_2->clear();
        ui->listWidget_2->addItem("0x00(组合有功总)");
        ui->listWidget_2->addItem("0x01(正向有功总)");
        ui->listWidget_2->addItem("0x02(反向有功总)");
        ui->listWidget_2->addItem("0x03(组合无功1总)");
        ui->listWidget_2->addItem("0x04(组合无功2总)");
        ui->listWidget_2->addItem("0x05(一象限无功总)");
        ui->listWidget_2->addItem("0x06(二象限无功总)");
        ui->listWidget_2->addItem("0x07(三象限无功总)");
        ui->listWidget_2->addItem("0x08(四象限无功总)");
        ui->listWidget_2->addItem("0x09(正向视在总)");
        ui->listWidget_2->addItem("0x0a(反向视在总)");
        ui->listWidget_2->addItem("");
        ui->listWidget_2->addItem("0x15(A相正向有功)");
        ui->listWidget_2->addItem("0x16(A相反向有功)");
        ui->listWidget_2->addItem("0x17(A相组合无功1)");
        ui->listWidget_2->addItem("0x18(A相组合无功2)");
        ui->listWidget_2->addItem("0x19(A相1象限无功)");
        ui->listWidget_2->addItem("0x1a(A相2象限无功)");
        ui->listWidget_2->addItem("0x1b(A相3象限无功)");
        ui->listWidget_2->addItem("0x1c(A相4象限无功)");
        ui->listWidget_2->addItem("");
        ui->listWidget_2->addItem("0x29(B相正向有功)");
        ui->listWidget_2->addItem("0x2a(B相反向有功)");
        ui->listWidget_2->addItem("0x2b(B相组合无功1)");
        ui->listWidget_2->addItem("0x2c(B相组合无功2)");
        ui->listWidget_2->addItem("0x2d(B相第一象限无功)");
        ui->listWidget_2->addItem("0x2e(B相第2象限无功)");
        ui->listWidget_2->addItem("0x2f(B相第3象限无功)");
        ui->listWidget_2->addItem("0x30(B相第4象限无功)");
        ui->listWidget_2->addItem("");
        ui->listWidget_2->addItem("0x3d(C相正向有功)");
        ui->listWidget_2->addItem("0x3e(C相反向有功)");
        ui->listWidget_2->addItem("0x3f(C相组合无功1)");
        ui->listWidget_2->addItem("0x40(C相组合无功2)");
        ui->listWidget_2->addItem("0x41(C相第1象限无功)");
        ui->listWidget_2->addItem("0x42(C相第2象限无功)");
        ui->listWidget_2->addItem("0x43(C相第3象限无功)");
        ui->listWidget_2->addItem("0x44(C相第4象限无功)");
        ui->listWidget_2->setCurrentRow(0);

        ui->listWidget_3->clear();
        ui->listWidget_3->addItem("0x00(有功总电能)");
        ui->listWidget_3->addItem("0x01(费率1电能)");
        ui->listWidget_3->addItem("0x02(费率2电能)");
        ui->listWidget_3->addItem("0x03(费率3电能)");
        ui->listWidget_3->addItem("0x04(费率4电能)");
        ui->listWidget_3->addItem("0xff(数据块)");
        ui->listWidget_3->setCurrentRow(0);

        ui->listWidget_4->clear();
        ui->listWidget_4->addItem("当前值");
        ui->listWidget_4->addItem("上1次");
        ui->listWidget_4->addItem("上2次");
        ui->listWidget_4->addItem("上3次");
        ui->listWidget_4->addItem("上4次");
        ui->listWidget_4->addItem("上5次");
        ui->listWidget_4->addItem("上6次");
        ui->listWidget_4->addItem("上7次");
        ui->listWidget_4->addItem("上8次");
        ui->listWidget_4->addItem("上9次");
        ui->listWidget_4->addItem("上10次");
        ui->listWidget_4->addItem("上11次");
        ui->listWidget_4->addItem("上12次");
        ui->listWidget_4->setCurrentRow(0);

    }
    else if(ui->listWidget->currentRow()==2)
    {
        ui->listWidget_2->clear();
        ui->listWidget_2->addItem("0x01(电压)");
        ui->listWidget_2->addItem("0x02(电流)");
        ui->listWidget_2->addItem("0x03(瞬时有功功率)");
        ui->listWidget_2->addItem("0x04(瞬时无功功率)");
        ui->listWidget_2->addItem("0x05(瞬时视在功率)");
        ui->listWidget_2->addItem("0x06(功率因数)");
        ui->listWidget_2->addItem("0x07(相角)");
        ui->listWidget_2->addItem("0x80(零线电流)");
        ui->listWidget_2->setCurrentRow(0);
    }
}

